package com.syt.cellphone.information.ui.widget

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.material.appbar.AppBarLayout
import com.syt.cellphone.information.R


/**
 *  @author: syt
 *  创建时间: 2020/9/8 15:19
 *  @功能   标题滑动行为控制
 *  @version v1.0
 */
class TransferHeaderBehavior @JvmOverloads constructor (
    context: Context,
    attrs: AttributeSet?= null
//) : CoordinatorLayout.Behavior<TextView>(context, attrs) {
) : AppBarLayout.ScrollingViewBehavior(context, attrs) {

    @SuppressLint("ResourceAsColor")
    override fun onDependentViewChanged(
        parent: CoordinatorLayout,
        child: View,
        dependency: View
    ): Boolean {
        val tvTitle = child.findViewById<TextView>(R.id.tv_details_title_name)
        val toolbar = dependency.findViewById<Toolbar>(R.id.toolbar)
        // 判断是否滑动到tv_title被隐藏，滑动到了标题栏就显示，没到就隐藏
        if (child.y <= toolbar.height + 20) {
            toolbar.title = tvTitle.text
        } else {
            toolbar.title = ""
        }
        return super.onDependentViewChanged(parent, child, dependency)
    }

    override fun onDependentViewRemoved(parent: CoordinatorLayout, child: View, dependency: View) {
        val toolbar = dependency.findViewById<Toolbar>(R.id.toolbar)
        toolbar.title = "无标题"
        super.onDependentViewRemoved(parent, child, dependency)
    }
}